from django.test import TestCase

# Create your tests here.
class CategoryListPageView(LoginRequiredMixin, ListView):
    ## login_url = "/account/login/"  和下边一行的功能是一样的,一个是硬编码,一个是灵活实现
    login_url = reverse_lazy('user_login') 
    template_name = "product/category-list.html"
    
    def getLayuiSelfTreeData(self):
        ## 生成Layui自带的树状结构 开始
        categorys = Category.objects.all()
        productTree = []
        for obj in  categorys:
            objNode = {}
            products = Product.objects.filter(category_id=obj.id)

            child = []
            for item in products:
                itemNode = {}
                itemNode['id'] = item.id
                itemNode['title'] = item.spec
                child.append(itemNode)

            objNode['id'] = obj.id
            objNode['title'] = obj.name
            objNode['children'] = child 
            productTree.append(objNode)
        return productTree
        ## 生成树状结构 结束 

    def getLayuiDtreeData(self):
        ## 生成Layui独立的dtree树状结构 开始
        resultdict = {}
        status = {}
        status['code'] = 200
        status['message'] = 'success' 

        categorys = Category.objects.all()
        dataArray = []
        for obj in  categorys:
            objNode = {}
            products = Product.objects.filter(category_id=obj.id)

            for item in products:
                itemNode = {}
                itemNode['id'] = str(item.id)
                itemNode['title'] = item.spec
                itemNode['checkArr'] = '0'
                itemNode['parentId'] = str(obj.id)
                dataArray.append(itemNode)

            objNode['id'] = str(obj.id)
            objNode['title'] = obj.name
            objNode['checkArr'] = '0'
            objNode['parentId'] = '0' 
            dataArray.append(objNode)
        
        resultdict['status'] = status
        resultdict['data'] = dataArray 
        print (resultdict)
        return resultdict 


    def get(self, request):
        productDtree = self.getLayuiDtreeData()
        productTree = self.getLayuiSelfTreeData()
        request.path = "/static/X-admin/lib" ##需要更改一下,要不然没办法取到dtree.js
        print("=================",request.path)
        return render(request, self.template_name, {"productTree": productTree,"productDtree": productDtree })

    def post(self, request, *args, **kwargs):
        ## 过滤条件参数
        ## 判断是搜索的数据还是直接显示的数据
        q = {}
        page = request.POST.get('page')
        rows = request.POST.get('limit')

        filter_name = request.POST.get('filter_name')         

        if filter_name:
            q['name__icontains'] = filter_name

        categorys = Category.objects.filter(**q)

        start = (int(page) - 1) * int(rows)
        end = (int(page) - 1) * int(rows) + int(rows)

        total = categorys.count()
        categorys = categorys[start:end]

        dict = []
        resultdict = {}

        for obj in categorys:
            dic = {}
            dic['id'] = obj.id
            dic['name'] = obj.name
            dic['created'] = obj.created.strftime("%Y-%m-%d %H:%M:%S")
            dict.append(dic)

        resultdict['code'] = 0
        resultdict['msg'] = ""
        resultdict['count'] = total 
        resultdict['data'] = dict

        return JsonResponse(resultdict, safe=False) 

